package com.cqan.manager;


import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cqan.entity.Atom;
import com.cqan.entity.AtomValue;
import com.cqan.entity.AtomField;
import com.cqan.entity.FieldType;
import com.cqan.entity.Report;
import com.cqan.exception.AppException;
import com.cqan.orm.Page;
import com.cqan.orm.PropertyFilter;
import com.cqan.orm.PropertyFilter.MatchType;
import com.cqan.service.AtomService;
import com.cqan.service.AtomValueService;
import com.cqan.service.AtomFieldService;
import com.cqan.service.ReportService;


@Service
@SuppressWarnings("restriction")
public class ReportManager {


	@Resource
	private ReportService reportService;
	
	@Resource
	private AtomValueService AtomValueService;
	
	@Resource
	private AtomService atomService;
	
	@Resource
	private AtomFieldService atomFieldService;

	/**
	 * 创建报表
	 * 
	 * @param name
	 * @return
	 */
	public Report createReport(String name) {
		Report report = new Report();
		report.setName(name);
		report.setState(true);
		report.setCtime(Calendar.getInstance().getTime());
		return report;
	}
	
	public Report getReport(Long id){
		
		return reportService.get(id);
	}
	
	public Report addField(Report report,String name,String attrName,FieldType fieldType)throws AppException{
		AtomField field = atomFieldService.findFieldByAttNameAndReportId(attrName, report.getId());
		if (field!=null) {
			Report r = field.getReport();
			if ( r!=null&& r.getId().equals(report.getId())) {
				throw new AppException("report.attr.exist" ,new Object[]{report.getName(),attrName});
			}
		}
		field = new AtomField(name, attrName, fieldType);
		field.setReport(report);
		atomFieldService.add(field);
		return report;
	}
	
	public Report addField(Long reportId,String name,String attrName,FieldType fieldType)throws AppException{
		Report report = reportService.get(reportId);
		return this.addField(report, name, attrName, fieldType);
	}
	
	public List<AtomField> reportFields(Long rid){
		
		return atomFieldService.findBy("report.id", rid, MatchType.EQ);
	}
	
	public Page<AtomField> reportFields(Long rid,Page<AtomField> page){
		
		return atomFieldService.reportFields(rid,page);
	}
	
	public Page<Atom> reportAtoms(Long rid,Page<Atom> page){
		
		return atomService.reportAtoms(rid,page);
	}
	
	public List<Atom> reportAtoms(Long rid){
		
		return atomService.findBy("report.id", rid, MatchType.EQ);
	}
	
	public void addAtom(){
		
	}
	
	
	
	public List<Report> reportList(){
		
		return reportService.getAll();
	}
	
	public Page<Report> reportList(Page<Report> page){
		
		return reportService.getAll(page);
	}
	
	public List<AtomField> fieldList(Report report){
		
		return atomFieldService.findBy("report", report, MatchType.EQ);
	}
	public List<Atom> atomList(Report report){
		
		return atomService.findBy("report", report, MatchType.EQ);
	}
	

	public Report save(Report report) {
		reportService.add(report);
		return report;
	}

	public Report update(Report report) {

		reportService.update(report);
		return report;
	}

	public void deleteReport(Long id) {
		reportService.delete(id);
	}

}
